package com.tj.jinzao.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.bumptech.glide.Glide;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseActivity;
import com.tj.jinzao.callback.IUploadView;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.fragment.PersonZXSEditFragment;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.Target;
import com.tj.jinzao.model.User;
import com.tj.jinzao.presenter.UserPresenter;
import com.tj.jinzao.utils.ToastUtils;
import com.tj.jinzao.widgets.MaxTextLengthFilter;
import com.tj.jinzao.widgets.RoundedImageView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

import static com.luck.picture.lib.config.PictureConfig.CHOOSE_REQUEST;

public class PersonZXSEditActivity extends BaseActivity implements IUploadView<List<User>> {
    @BindView(R.id.iv_head)
    RoundedImageView mHeadView;
    @BindView(R.id.et_name)
    EditText mNameView;
    @BindView(R.id.synopsis)
    EditText synopsis;
    @BindView(R.id.speciality)
    EditText speciality;
    private UserPresenter mPresenter;
    User user;
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) mPresenter.detachView();
    }


    @Override
    public int getLayoutResource() {
        return R.layout.activity_zxs_edit_message;
    }


    @Override
    protected void setupView() {
        user = UserManager.getInstance().getUser();
        mPresenter = new UserPresenter(this);
        Glide.with(this).load(Constant.IMAGE_PREFIX+user.getImgurl()).into(mHeadView);
        mNameView.setText(user.getNickname());
        mPresenter = new UserPresenter(this);
        mNameView.setFilters(new InputFilter[]{new MaxTextLengthFilter(12,PersonZXSEditActivity.this,"昵称限12字以内")});
        synopsis.setText(user.getSynopsis());
        speciality.setText(user.getSpeciality());
        findViewById(R.id.back_lay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        findViewById(R.id.config).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(TextUtils.isEmpty(synopsis.getText())){
                    ToastUtils.showToast(PersonZXSEditActivity.this,"请输入个人简介");
                    return;
                }else if(TextUtils.isEmpty(speciality.getText())){
                    ToastUtils.showToast(PersonZXSEditActivity.this,"请输入您的擅长");
                    return;
                }
                Log.w("数据更新","6666");

                upload();
            }
        });
    }
    @OnClick(R.id.ll_head_layout)
    public void chooseHead() {
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())
                .maxSelectNum(1)
                .compress(true)
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }
    private String mImagePath;
    private void upload() {
        User user = UserManager.getInstance().getUser();
        Map<String, Object> map = new HashMap();
        if (!TextUtils.isEmpty(mImagePath)) {
            if (mImagePath.startsWith(Constant.SD_ROOT)) {
                mPresenter.uploadFile(mImagePath);
                return;
            }
            map.put("imgurl", mImagePath);
        }
        String nickName = mNameView.getText().toString().trim();
        if (!TextUtils.equals(nickName, user.getNickname())) {
            map.put("nickname", nickName);
        }
        String synop = synopsis.getText().toString().trim();
        if (!TextUtils.equals(synop, user.getSynopsis())) {
            map.put("synopsis", synop);
        }
        String specia = speciality.getText().toString().trim();
        if (!TextUtils.equals(specia, user.getSpeciality())) {
            map.put("speciality", specia);
        }

        Log.w("用户信息图片上传",""+map.toString());

        if (map.size() > 0) {
            mPresenter.updateInfo(PersonZXSEditActivity.this,map);
        } else {
            super.clickLeftButton();
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case CHOOSE_REQUEST:
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    for (LocalMedia localMedia : selectList) {
                        mImagePath = localMedia.isCompressed() ? localMedia.getCompressPath() : localMedia.getPath();
                        Glide.with(this).load(mImagePath).into(mHeadView);
                    }
                    break;
            }
        }
    }

    @Override
    public void onUploadSuccess(String path, String url) {
        mImagePath = url;
        upload();

    }

    @Override
    public void onCaseSuccess(List<Target> list) {

    }

    @Override
    public void onCaseFailed(String msg) {

    }

    @Override
    public Activity getContext() {
        return null;
    }

    @Override
    public void onRequestSuccess(List<User> users) {
        ToastUtils.showToast(this, "更新成功");
        finish();

    }

    @Override
    public void onRequestNoData(String message) {

    }

    @Override
    public void onRequestError(String error_message) {

    }
}
